home *** CD-ROM | disk | FTP | other *** search
- /* pty.c - This module contains the code to handle the pty stuff.
-
- Copyright 1989 by Jeffrey F. Lawhorn (jeffl@berick.uucp)
-
- This file is part of vuser.
-
- vuser is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your
- option) any later version.
-
- vuser is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNU CC; see the file COPYING. If not, write to the
- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #if !defined(lint)
- static char SCCSid[] = "$Id: pty.c,v 1.1 89/12/15 21:31:03 jeffl Exp $";
- #endif
-
- #include <stdio.h>
- #include <fcntl.h>
-
- #if defined(__STDC__)
- extern void Tty2ShellMode();
- #else
- extern void Tty2ShellMode();
- #endif
-
- #if defined(SYSV)
- #include <termio.h>
- struct termio ShellTtyMode;
- #else
- #include <sgtty.h>
- struct sgttyb ShellTtyMode;
- #endif
-
- int MasterPty = -1, SlavePty = -1;
-
- void Pty2ShellMode()
- {
- #if defined(SYSV)
- ioctl(SlavePty, TCSETA, &ShellTtyMode);
- #else
- ioctl(SlavePty, TIOCSETP, &ShellTtyMode);
- #endif
- return;
- }
-
- void GetPty()
- {
- int line = 'p', node = 0;
- char tmp[32];
-
- strcpy(tmp, "/dev/ptyxx");
- for(; line <= 'q'; ++line)
- for(node = 0; node < 16; ++node) {
- tmp[8] = (char)line;
- tmp[9] = (char)((node > 9 ? (node + 'a' - 10) : (node + '0')));
- if((MasterPty = open(tmp, O_RDWR|O_NDELAY)) >= 0) {
- tmp[5] = 't';
- if((SlavePty = open(tmp, O_RDWR)) < 0) {
- perror("on pty: ");
- Tty2ShellMode();
- exit(1);
- }
- Pty2ShellMode();
- return;
- }
- }
- fprintf(stderr, "No ptys available, try later.\n");
- Tty2ShellMode();
- exit(1);
- }
-
- void SlavePty2Stdio()
- {
- int cnt = 3;
- #if !defined(_NFILE)
- int maxFiles = getdtablesize();
- #else
- int maxFiles = _NFILE;
- #endif
-
- close(0);
- dup(SlavePty);
- close(1);
- dup(SlavePty);
- close(2);
- dup(SlavePty);
- for(; cnt < maxFiles; ++cnt)
- close(cnt);
- return;
- }
-